package com.example.managemyself.ui.diary;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;

import com.example.managemyself.R;
import com.example.managemyself.data.model.DiaryEntry;
import com.example.managemyself.viewmodel.DiaryViewModel;

import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * 日记详情Activity
 * 显示日记的详细内容，支持查看和编辑
 */
public class DiaryDetailActivity extends AppCompatActivity {
    
    public static final String EXTRA_DIARY_ID = "diary_id";
    
    private DiaryViewModel diaryViewModel;
    private TextView tvTitle;
    private TextView tvDate;
    private TextView tvContent;
    private TextView tvMood;
    private TextView tvWeather;
    private TextView tvLocation;
    private Toolbar toolbar;
    
    private int diaryId = -1;
    private DiaryEntry currentDiary;
    
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 EEEE", Locale.getDefault());
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary_detail);
        
        // 获取传递的日记ID
        Intent intent = getIntent();
        if (intent.hasExtra(EXTRA_DIARY_ID)) {
            diaryId = intent.getIntExtra(EXTRA_DIARY_ID, -1);
        }
        
        if (diaryId == -1) {
            Toast.makeText(this, "日记信息错误", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        
        initViews();
        setupToolbar();
        initViewModel();
        loadDiaryData();
    }
    
    private void initViews() {
        toolbar = findViewById(R.id.toolbar);
        tvTitle = findViewById(R.id.tv_diary_title);
        tvDate = findViewById(R.id.tv_diary_date);
        tvContent = findViewById(R.id.tv_diary_content);
        tvMood = findViewById(R.id.tv_diary_mood);
        tvWeather = findViewById(R.id.tv_diary_weather);
        tvLocation = findViewById(R.id.tv_diary_location);
    }
    
    private void setupToolbar() {
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("日记详情");
        }
    }
    
    private void initViewModel() {
        diaryViewModel = new ViewModelProvider(this).get(DiaryViewModel.class);
    }
    
    private void loadDiaryData() {
        diaryViewModel.getDiaryEntryById(diaryId).observe(this, diaryEntry -> {
            if (diaryEntry != null) {
                currentDiary = diaryEntry;
                displayDiaryData(diaryEntry);
            } else {
                Toast.makeText(this, "找不到该日记", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
    
    private void displayDiaryData(DiaryEntry diary) {
        // 设置标题
        tvTitle.setText(diary.getTitle());
        
        // 设置日期
        if (diary.getDate() != null) {
            tvDate.setText(dateFormat.format(diary.getDate()));
        }
        
        // 设置内容
        tvContent.setText(diary.getContent());
        
        // 设置心情
        if (diary.getMood() != null && !diary.getMood().isEmpty()) {
            tvMood.setText("心情: " + diary.getMood());
            tvMood.setVisibility(android.view.View.VISIBLE);
        } else {
            tvMood.setVisibility(android.view.View.GONE);
        }
        
        // 设置天气
        if (diary.getWeather() != null && !diary.getWeather().isEmpty()) {
            tvWeather.setText("天气: " + diary.getWeather());
            tvWeather.setVisibility(android.view.View.VISIBLE);
        } else {
            tvWeather.setVisibility(android.view.View.GONE);
        }
        
        // 设置位置
        if (diary.getLocation() != null && !diary.getLocation().isEmpty()) {
            tvLocation.setText("位置: " + diary.getLocation());
            tvLocation.setVisibility(android.view.View.VISIBLE);
        } else {
            tvLocation.setVisibility(android.view.View.GONE);
        }
        
        // 设置创建和更新时间信息
        StringBuilder timeInfo = new StringBuilder();
        if (diary.getCreatedDate() != null) {
            timeInfo.append("创建: ").append(timeFormat.format(diary.getCreatedDate()));
        }
        if (diary.getUpdatedDate() != null && !diary.getUpdatedDate().equals(diary.getCreatedDate())) {
            if (timeInfo.length() > 0) {
                timeInfo.append(" | ");
            }
            timeInfo.append("更新: ").append(timeFormat.format(diary.getUpdatedDate()));
        }
        
        // 可以添加一个时间信息的TextView来显示这些信息
        if (getSupportActionBar() != null && timeInfo.length() > 0) {
            getSupportActionBar().setSubtitle(timeInfo.toString());
        }
    }
    
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
} 